//
//  WFPicScrollVC.m
//  谷居
//
//  Created by dllo on 16/7/2.
//  Copyright © 2016年 刘万方. All rights reserved.
//

#import "WFPicScrollVC.h"
#import "Masonry.h"
#import "WFPhotoLayouy.h"
#import "WFPhotoCell.h"
#import "WFPhotosModel.h"
@interface WFPicScrollVC ()<WFPhotoLayouyDelegtate, UICollectionViewDelegate, UICollectionViewDataSource>;

@property (nonatomic, retain) UIButton *leftBarButton;

@property (nonatomic, retain) UIButton *rightBarButton;

@property (nonatomic, retain) UIView *nameVeiw;

@property (nonatomic, retain) UICollectionView *collectionView;

@end

@implementation WFPicScrollVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor blackColor];
    
    [self createCollection];
    [self createButton];

    // Do any additional setup after loading the view.
}


/** 创建左右button. */
- (void)createButton {
    _leftBarButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:_leftBarButton];
    [_leftBarButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.view).offset(16);
        make.top.equalTo(self.view).offset(26);
        make.size.mas_equalTo(CGSizeMake(32, 32));
    }];
    [_leftBarButton addTarget:self action:@selector(pop) forControlEvents:UIControlEventTouchUpInside];
    [_leftBarButton setBackgroundImage:[UIImage imageNamed:@"返回2.png"] forState:UIControlStateNormal];
    
    _rightBarButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:_rightBarButton];
    [_rightBarButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.equalTo(self.view).offset(-16);
        make.top.equalTo(self.view).offset(26);
        make.size.mas_equalTo(CGSizeMake(32, 32));
    }];
    [_rightBarButton addTarget:self action:@selector(share) forControlEvents:UIControlEventTouchUpInside];
    
    [_rightBarButton setBackgroundImage:[UIImage imageNamed:@"分享蓝.png"] forState:UIControlStateNormal];
}

- (void)createCollection {
    WFPhotoLayouy *layout = [[WFPhotoLayouy alloc] init];
    layout.count = self.picArray.count;
    layout.delegate = self;
    
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    _collectionView = [[UICollectionView alloc] initWithFrame:[[UIScreen mainScreen]bounds] collectionViewLayout:layout];
    [self.view addSubview:_collectionView];
    [_collectionView release];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    [_collectionView registerClass:[WFPhotoCell class] forCellWithReuseIdentifier:@"photo"];
    _collectionView.pagingEnabled = YES;
    
    _collectionView.contentOffset = CGPointMake(SCREEN_WIDTH * self.index, 0);
}

#pragma mark - 自定义layout的协议方法
- (CGFloat)layout:(WFPhotoLayouy *)layout heightForItemAtIndexPath:(NSIndexPath *)indexPath Width:(CGFloat)width {
    WFPhotosModel *model = self.picArray[indexPath.row];
    CGFloat height = width *model.height / model.width;
    return height;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.picArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    WFPhotoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"photo" forIndexPath:indexPath];
    cell.model = self.picArray[indexPath.row];
    return cell;
}
#pragma mark - 点击方法
- (void)pop {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)share {
    NSLog(@"点击分享按钮");
}


/** 创建下面view. */
- (void)createNameView {
    _nameVeiw = [[UIView alloc] init];
    [self.view addSubview:_nameVeiw];
    [_nameVeiw release];
    [_nameVeiw mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.equalTo(self.view);
        make.height.mas_equalTo(40);
        
    }];
    _nameVeiw.backgroundColor = [UIColor clearColor];
    
    
}

- (void)viewWillAppear:(BOOL)animated {
    self.navigationController.navigationBar.hidden = YES;
    
    self.tabBarController.tabBar.hidden = YES;

}

- (void)viewWillDisappear:(BOOL)animated {
    self.navigationController.navigationBar.hidden = NO;
    self.tabBarController.tabBar.hidden = NO;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
